import requests
import allure
import pytest
import threading
from requests.auth import HTTPDigestAuth


from app.Seetrum import Seetrum
from data.detectorParms import DetectorParams
thread_nums = 4

detectorParamsTitle = DetectorParams().detectorParamsTitle
detectorParamsData = DetectorParams().detectorParamsData


class TestConfigs:
    data = [('admin', 'U2VldHJ1bTg4OA==')]
    data1 = ['正常登录场景']

    def setup_class(self):
        self.Seetrum = Seetrum()

        self.detector = self.Seetrum.get_detectorParams()['data']
        self.detectorPost = {}
        self.code = ''

    def teardown(self):
        self.detectorGet = self.Seetrum.get_detectorParams()['data']

        print('get 取到的数据:', self.detectorGet)

        if self.code == '0':
            for key, value in self.detectorPost.items():
                print(key, value)
                assert round(value, 2) == self.detectorPost[key]
                print("get : ", key, "等于", value)
                print("post: ", key, "等于", self.detectorPost[key])

    @pytest.mark.parametrize('username,password', data, ids=data1)
    def test_login(self, username, password):
        url = "https://169.254.169.1/v2/account/login"
        params = {
            "username": username,
            "password": password
        }
        # r=requests.get("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wwd429890af578a469&corpsecret=ooGtHdJ4_w8wn3auYijd-QO9SQmObz1GLBQ7mtW3J74")
        # r = requests.get(url,params=params)
        r = requests.post(url, json=params, verify=False, auth=HTTPDigestAuth('admin', 'Seetrum888'))
        print(r.json())
        # assert r.json()['code'] == "0"
        # assert r.json()['msg'] == "ok"

    # 导入配置

    # def test_configs(self):
    #     url = "https://169.254.169.1/v2/api/configs"
    #     files = {'file': open('D:/report/downloads_configs (3).yml', 'rb')}
    #
    #     r = requests.post(url, verify=False, auth=HTTPDigestAuth('admin', 'Seetrum888', ), files=files)
    #     print(r.json())
    #     assert r.json()['code'] == "0"
    #     assert r.json()['msg'] == "ok"

    # 设置检测配置参数 ---  测试数据

    # detectorParamsAssert: list = ['0']

    # 设置检测配置参数

    @pytest.mark.parametrize('data', detectorParamsData, ids=detectorParamsTitle)
    # @allure.title("kpi:设置检测配置参数")
    def test_postDetectorParams(self, data):
        url = "https://169.254.169.1/v2/api/detectorParams"
        params = {}
        for key, value in data.items():
            if key not in ["code", "msg"]:
                params[key] = value

        r = requests.post(url, json=params, verify=False, auth=HTTPDigestAuth('admin', 'Seetrum888', ))
        print(r.json())
        print("post的数据=", params)
        assert r.json()['code'] == data['code']
        assert r.json()['msg'] == data['msg']
        self.code = data['code']
        self.detectorPost = params



